package com.bestcem.xm.member.mq.receiver;//package com.bestcem.xm.member.mq.receiver;
//
//import com.bestcem.xm.config.RabbitMqConstant;
//import com.bestcem.xm.member.mq.message.MemberSliceMessage;
//import com.bestcem.xm.member.service.SendMemberService;
//import com.bestcem.xm.util.ErrorCodes;
//import com.bestcem.xm.util.MsgPackFormatUtil;
//import com.bestcem.xm.common.core.domain.web.ServiceResult;
//import com.rabbitmq.client.Channel;
//import lombok.extern.slf4j.Slf4j;
//import org.apache.commons.lang3.StringUtils;
//import org.springframework.amqp.core.Message;
//import org.springframework.amqp.rabbit.annotation.RabbitListener;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.stereotype.Component;
//
//import java.io.IOException;
//
///**
// * @author ming.xue <ming.xue@idiaoyan.com>
// * @desc 联系人切片消息队列监听
// * @date 2021-07-22
// */
//@Slf4j
//@Component
//public class MemberSliceReceiver {
//    /**
//     * 推送联系人服务
//     */
//    @Autowired
//    private SendMemberService sendMemberService;
//
//    /**
//     * 处理联系人切片消息
//     *
//     * @param message
//     * @param channel
//     * @throws IOException
//     */
//    @RabbitListener(queues = "#{memberSliceQueue.name}", ackMode = "MANUAL",
//            concurrency = "${app.member.send-member-config.receiver-consume-concurrency:2}",
//            containerFactory = "memberSliceContainerFactory")
//    public void receive(Message message, Channel channel) throws IOException {
//        long deliveryTag = message.getMessageProperties().getDeliveryTag();
//        try {
//            MsgPackFormatUtil.MqMessage mqMessage = MsgPackFormatUtil.mqDecode(message.getBody());
//            if (mqMessage == null) {
//                log.error("receiver decode member slice failed, abandon message, message: {}", message);
//                channel.basicNack(deliveryTag, false, false);
//                return;
//            }
//            if (log.isInfoEnabled()) {
//                log.info("received member slice: {}", mqMessage);
//            }
//
//            MemberSliceMessage memberSliceMessage = MsgPackFormatUtil.mapToObj(mqMessage.getData(),
//                    MemberSliceMessage.class);
//            ServiceResult<Void> result = sendMemberService.sendMembers(memberSliceMessage);
//            if (result.isSuccess()) {
//                channel.basicAck(deliveryTag, false);
//                if (log.isInfoEnabled()) {
//                    log.info("receiver process member slice success, slice: {}", memberSliceMessage);
//                }
//            } else {
//                log.error("receive member slice failed, reason: {}, message: {}", result,
//                        memberSliceMessage);
//
//                // 需要重试
//                if (StringUtils.equals(result.getErrorCode(), ErrorCodes.B0002)) {
//                    channel.basicNack(deliveryTag, false, true);
//                }
//            }
//        } catch (Exception e) {
//            log.error("receive member slice failed, message: {}", message, e);
//            channel.basicNack(deliveryTag, false, false);
//        }
//    }
//}
